Partial Public Class FileManagerView

    Friend presenter As FileManagerViewPresenter

    Private Sub FileManagerView_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.presenter = New FileManagerViewPresenter(My.Forms.MainForm, Me)

    End Sub

    Private Sub FolderTreeView_NodeMouseClick(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles FolderTreeView.NodeMouseClick


        Me.presenter.BeginGetChildFolders(e.Node)
        Me.presenter.BeginGetFilesByFolder(Convert.ToInt32(e.Node.Tag))

    End Sub

    Private Sub FileListView_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.LabelEditEventArgs) Handles FileListView.AfterLabelEdit

        Me.presenter.BeginAddFolder(e.Label)


    End Sub


    Private Sub FileListView_ColumnClick _
        (ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) _
        Handles FileListView.ColumnClick


        Me.presenter.ReorderFileList(e)

    End Sub




    Private Sub FileListView_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FileListView.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then

            Me.presenter.ShowFileContextMenu(e.Location)


        End If
    End Sub

    Private Sub DeleteFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteFileToolStripMenuItem.Click


        Me.presenter.BeginDeleteSelectedFiles()

    End Sub

    Private Sub AddFolderToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFolderToolStripMenuItem.Click

        Me.presenter.AddNewFolderItem()

    End Sub

    Friend Sub UploadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles UploadFileToolStripMenuItem.Click

        If Not Me.UploadBackgroundWorker.IsBusy Then

            Dim uploadFileDialog As New OpenFileDialog
            uploadFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
            uploadFileDialog.Filter = "All Files (*.*)|*.*"

            uploadFileDialog.Multiselect = True


            If uploadFileDialog.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then

                Dim fileNames As New List(Of String)(uploadFileDialog.FileNames)

                Dim folderId As Integer = -1

                If Me.FolderTreeView.SelectedNode IsNot Nothing Then

                    folderId = Convert.ToInt32(Me.FolderTreeView.SelectedNode.Tag)

                End If

                Me.UploadBackgroundWorker.RunWorkerAsync(New Object() {folderId, fileNames})

            End If

        Else
            MessageBox.Show("Already uploading files, please wait for current files to complete and try again.", "Busy", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If

    End Sub

    Friend Sub DownloadFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFileToolStripMenuItem.Click, FileListView.DoubleClick

        If Not Me.DownloadBackgroundWorker.IsBusy Then

            My.Forms.MainForm.DownloadFolderBrowserDialog.SelectedPath = Me.presenter.GetDownloadFolder

            If My.Forms.MainForm.DownloadFolderBrowserDialog.ShowDialog = DialogResult.OK Then

                Dim downloadPath As String = My.Forms.MainForm.DownloadFolderBrowserDialog.SelectedPath

                If String.IsNullOrEmpty(downloadPath) Then

                    downloadPath = Me.presenter.GetDownloadFolder

                End If


                Dim fileList As New Generic.Dictionary(Of Integer, String)

                For Each item As ListViewItem In Me.FileListView.SelectedItems

                    fileList.Add(CInt(item.Tag), IO.Path.Combine(downloadPath, item.Text))

                Next


                Me.DownloadBackgroundWorker.RunWorkerAsync(fileList)

            End If

        Else

            MessageBox.Show("Already downloading, please wait for current files to complete and try again.", "Busy", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If

    End Sub

    Private Sub PropertiesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PropertiesToolStripMenuItem.Click

        Me.presenter.BeginGetInfo()


    End Sub




    Private Sub DownloadBackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles DownloadBackgroundWorker.DoWork

        Dim args As Generic.Dictionary(Of Integer, String)

        args = TryCast(e.Argument, Generic.Dictionary(Of Integer, String))

        If args IsNot Nothing Then

            Me.presenter.DownloadFiles(DirectCast(e.Argument, Generic.Dictionary(Of Integer, String)))

        End If

    End Sub

    Private Sub DownloadBackgroundWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles DownloadBackgroundWorker.ProgressChanged

        Dim args As FileManagerEventArgs = TryCast(e.UserState, FileManagerEventArgs)

        If args IsNot Nothing Then

            Me.presenter.SetStatus(args)

        End If

    End Sub
    Private Sub UploadBackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles UploadBackgroundWorker.DoWork

        Dim args As Object() = TryCast(e.Argument, Object())

        If args IsNot Nothing Then

            Dim folderId As Integer = Convert.ToInt32(args(0))

            Me.presenter.UploadFiles(folderId, DirectCast(args(1), List(Of String)))


        End If


    End Sub

    Private Sub UploadBackgroundWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles UploadBackgroundWorker.ProgressChanged

        Dim args As FileManagerEventArgs = TryCast(e.UserState, FileManagerEventArgs)

        If args IsNot Nothing Then

            Me.presenter.SetStatus(args)

            If Me.FolderTreeView.SelectedNode IsNot Nothing _
                AndAlso CInt(Me.FolderTreeView.SelectedNode.Tag) = args.FolderId Then

                Me.presenter.RefreshFiles()

            End If

        Else

            Debug.WriteLine(e.UserState.ToString)


        End If

    End Sub

    Private Sub RefreshToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshToolStripMenuItem.Click

        Me.presenter.RefreshFiles()

    End Sub
End Class
